package com.bifrost.admin.controller;

import com.bifrost.model.ResultModel;
import com.bifrost.model.option.OptionModel;
import com.bifrost.service.ProductOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @com.bifrost.admin.controller
 * @wechat-mall
 * @lee Create Class on 2018/4/9
 */
@RestController
@RequestMapping("/admin/options")
@Api("/后台规格相关接口")
@Validated
public class ProductOptionController {

    @Autowired
    ProductOptionService productOptionService;


    @ApiOperation(value = "获取所有规格数据 / 根据id获取规格数据",notes = "optionId:{根据id 获取option规格下的规格值}")
    @ApiImplicitParam(name = "optionId" , value = "规格Id" , required = false , dataType = "Long" , paramType = "query")
    @GetMapping("/tree")
    public ResultModel findSkuOptions(Long optionId){
        List list;
        Assert.notEmpty(list = productOptionService.getOptionVlauesTree(optionId),"没有规格数据");
        return ResultModel.ok().put("result",list);
    }

    @ApiOperation("根据规格名称查询规格信息")
    @GetMapping("/name")
    public ResultModel findOptionByOptioname(@RequestParam @NotBlank(message = "规格名称不为空") String optionName){

        OptionModel optionModel = productOptionService.findOptionByName(optionName);
        return ResultModel.ok().put("result",optionModel);

    }

    @ApiOperation(value = "添加规格分类")
    @PostMapping("/add")
    public ResultModel addOption(@RequestBody @Valid OptionModel optionModel){

            Assert.isNull(optionModel.getProductOptionId(),"规格分类id必须为空");
            productOptionService.newOption(optionModel);
            return ResultModel.ok();
    }

    @ApiOperation(value = "修改规格分类")
    @PostMapping("/update")
    public ResultModel updateOption(@RequestBody @Valid OptionModel optionModel){

            productOptionService.mergeOption(optionModel);
            return ResultModel.ok();
    }


    @ApiOperation(value = "删除规格分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "optionId" , value = "规格Id" , required = true , dataType = "Long" , paramType = "query")})
    @GetMapping("/delete")
    public ResultModel deleteOption(@RequestParam @NotNull(message = "规格分类不能为空") Long optionId){

            productOptionService.deleteOption(optionId);
                return ResultModel.ok("删除成功");
    }

    @ApiOperation(value = "删除规格属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "optionValueId" , value = "规格属性Id" , required = true , dataType = "Long" , paramType = "query")})
    @GetMapping("/value/delete")
    public ResultModel deleteOptionValue(@RequestParam @NotNull(message = "规格属性不能为空")  Long optionValueId){

            productOptionService.deleteOptionValue(optionValueId);
                return ResultModel.error("删除成功");
    }

}
